//
//  AppDelegate.m
//  OurTourism
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "AppDelegate.h"
#import "DestinationViewController.h"
#import "RecommendViewController.h"
#import "GuideViewController.h"
#import "UserViewController.h"
#import <BmobSDK/Bmob.h>
#import "CoreDataManager.h"
#import "Header.h"
#import "APService.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    } else {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                       UIRemoteNotificationTypeSound |
                                                       UIRemoteNotificationTypeAlert)
                                           categories:nil];
    }
    
    // Required
    [APService setupWithOption:launchOptions];
    
    [Bmob registerWithAppKey:@"3955f36f11895cc20b4e8ca932e970d2"];
    [UMSocialData setAppKey:@"5661bb71e0f55a4f60003bca"];
    
    // 目的地
    DestinationViewController *destin = [[DestinationViewController alloc] init];
    UINavigationController *dest = [[UINavigationController alloc] initWithRootViewController:destin];
    UIImage *destImg = [UIImage imageNamed:@"iconfont-tuijian"];
    destImg = [destImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    dest.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:destImg tag:10000];
    
    // 推荐
    RecommendViewController *recomm = [[RecommendViewController alloc] init];
    UINavigationController *recom = [[UINavigationController alloc] initWithRootViewController:recomm];
    UIImage *recomImg = [UIImage imageNamed:@"iconfont-mudedidangqian"];
    recomImg = [recomImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    recom.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:recomImg tag:10001];
    
    // 指南
    GuideViewController *guide = [[GuideViewController alloc] init];
    UINavigationController *guid = [[UINavigationController alloc] initWithRootViewController:guide];
    UIImage *guideImg = [UIImage imageNamed:@"iconfont-gonglve"];
    guideImg = [guideImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    guid.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"攻略" image:guideImg tag:10002];
   
    // 用户
    UserViewController *user = [[UserViewController alloc] init];
    UINavigationController *use = [[UINavigationController alloc] initWithRootViewController:user];
    UIImage *userImg = [UIImage imageNamed:@"iconfont-wo"];
    userImg = [userImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    use.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:userImg tag:10002];
    
    
    UITabBarController *tab = [[UITabBarController alloc]init];
    tab = [[UITabBarController alloc]init];
    tab.tabBar.barTintColor = [UIColor colorWithRed:62 / 255. green:176 / 255. blue:192 / 255. alpha:1];
    tab.viewControllers = @[dest, recom, guid, use];
 
    self.window.rootViewController = tab;
    
    
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required
    [APService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
  //  [self saveContext];
}


@end
